package cn.wen.base.adapter.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.wen.base.vlayout.DelegateAdapter;
import cn.wen.base.vlayout.LayoutHelper;

/**
 * RecyclerView的通用的适配器
 */

public abstract class QuickVAdapter<T> extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {
    protected List<T> mData;
    protected int mLayoutId;
    private LayoutHelper mLayoutHelper;

    protected QuickVAdapter() {
    }

    public QuickVAdapter(List<T> data, LayoutHelper layoutHelper, int layoutId) {
        this.mData = data == null ? new ArrayList<T>() : new ArrayList<>(data);
        this.mLayoutHelper = layoutHelper;
        this.mLayoutId = layoutId;
    }

    public void setData(List<T> data) {
        mData = data;
    }

    public void setLayoutId(int layoutId) {
        mLayoutId = layoutId;
    }

    public void setLayoutHelper(LayoutHelper layoutHelper) {
        mLayoutHelper = layoutHelper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 如果是多条目，viewType就是布局ID
        View view = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
        QuickRecyclerViewHolder holder = new QuickRecyclerViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    protected void onBindViewHolderWithOffset(RecyclerView.ViewHolder holder, int position, int offsetTotal) {
        if (holder instanceof QuickRecyclerViewHolder) {
            if (position>mData.size()-1){
                return;
            }
            convert((QuickRecyclerViewHolder) holder, mData.get(position), position);
        }
    }

    /**
     * 绑定View的数据
     */
    protected abstract void convert(QuickRecyclerViewHolder holder, T item, int position);


    //==========================================数据相关================================================
    public void add(T elem) {
        mData.add(elem);
        notifyDataSetChanged();
    }

    public void addAll(List<T> data) {
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public void addFirst(T elem) {
        mData.add(0, elem);
        notifyDataSetChanged();
    }

    public void set(T oldElem, T newElem) {
        set(mData.indexOf(oldElem), newElem);
    }

    public void set(int index, T elem) {
        mData.set(index, elem);
        notifyDataSetChanged();
    }

    public void remove(T elem) {
        mData.remove(elem);
        notifyDataSetChanged();
    }

    public void remove(int index) {
        mData.remove(index);
        notifyItemRemoved(index);
    }

    public void replaceAll(List<T> elem) {
        mData.clear();
        mData.addAll(elem);
        notifyDataSetChanged();
    }

    public boolean contains(T elem) {
        return mData.contains(elem);
    }

    /**
     * 清除
     */
    public void clear() {
        mData.clear();
        notifyDataSetChanged();
    }

    public List<T> getData() {
        return mData;
    }


}
